package clone;
//深拷贝
/*//尤里
class Yuri implements Cloneable{//标记被克隆的尤里真身
    public String name = "尤里";
    public String action = "正常";

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    @Override
    public String toString() {
        return "Yuri{" +
                "name='" + name + '\'' +
                ", action='" + action + '\'' +
                '}';
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Yuri yuri = new Yuri();
        Yuri yuriclone1 =(Yuri) yuri.clone();//要抛一个异常，由于是Object类，向下转型要进行强制类型转化
        System.out.println(yuri);
        System.out.println(yuriclone1);
        System.out.println("--------------分割线--------------");
        yuriclone1.action = "受伤";
        System.out.println(yuri);
        System.out.println(yuriclone1);
    }
}*/

//深拷贝
//状态
class Action implements Cloneable{
    public String action = "正常";

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
//尤里
class Yuri implements Cloneable{//标记被克隆的尤里真身
    public String name = "尤里";
    public Action action = new Action();

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Yuri tmpclone = (Yuri) super.clone();//对尤里的克隆
        tmpclone.action = (Action) this.action.clone();//对状态的克隆
        return tmpclone;
    }
    @Override
    public String toString() {
        return "Yuri{" +
                "name='" + name + '\'' +
                ", action='" + action.action + '\'' +
                '}';
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Yuri yuri = new Yuri();
        Yuri yuriclone1 =(Yuri) yuri.clone();//要抛一个异常，由于是Object类，向下转型要进行强制类型转化
        Yuri yuriclone2 =(Yuri) yuri.clone();
        System.out.println(yuri);
        System.out.println(yuriclone1);
        System.out.println(yuriclone2);
        System.out.println("--------------分割线--------------");
        yuri.action.action = "受伤";//一个受伤，其他跟着受伤
        System.out.println(yuri);
        System.out.println(yuriclone1);
        System.out.println(yuriclone2);
    }
}